package com.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.constant.MessageConstant;
import com.exception.CustomException;
import com.pojo.Menu;
import com.pojo.Role;
import com.result.Result;
import com.service.MenuService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    /**
     * IsId：页面传过来的id进行判断是父菜单还是子菜单
     * ParentID：添加子菜单时传过来的父菜单
     * */

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody Menu menu){
        try {
            menuService.update(menu);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_MENU_FAIL);
    }




    //根据id查询
    @RequestMapping("/queryById")
    public Result queryById(Integer id){
        try {
            Map map=new HashMap();
            Menu menu = menuService.queryById(id);
            if(menu!=null && menu.getParentmenuid()!=null){
                Menu menuParent = menuService.queryById(menu.getParentmenuid());
                map.put("menuParent",menuParent);
            }
            map.put("menu",menu);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_MENU_FAIL);
    }


    //添加
    @PostMapping("/add")
    public Result add(@RequestBody Menu menu,Integer IsId){
//        System.out.println("进来了");
        try {
            if(IsId==1){//添加父菜单
                menu.setLevel(1);
                menuService.add(menu);
            }else{//添加子菜单
                if(menu.getParentmenuid()==null){
                    return new Result(false, "请选择一个父菜单");
                }
                menu.setLevel(2);
                menuService.add(menu);
            }
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_MENU_FAIL);
    }


    /*删除*/
    @PostMapping("/delete")
    public Result delete(Integer id) throws CustomException {
        menuService.delete(id);
        return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
    }

    //查询所有
    @RequestMapping("/queryAll")
    public Result queryAll(){
        try {
            List<Menu> list = menuService.queryAlll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_MENU_FAIL);
    }


    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Menu> list = menuService.queryAll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_MENU_FAIL);
    }

    //查询所有父菜单
    @RequestMapping("/findByParent")
    public Result findByParent(){
        try {
            List<Menu> list = menuService.queryByParent();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_MENU_FAIL);
    }




}
